#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fir first
#define sec second
#define chmin(a,b) (a=min(a,b))
#define chmax(a,b) (a=max(a,b))
int n,dp[200010][2],m,a[200010],b[200010],tag[2],mx[2];
void solve()
{
    scanf("%lld",&n);
    m=0;
    for(int i=1;i<=n;i++)scanf("%lld",a+i),b[i]=a[i];
    sort(b+1,b+n+1);
    m=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+m+1,a[i])-b;
    memset(dp,-0x3f,sizeof(dp));
    dp[0][0]=dp[0][1]=0;
    tag[0]=tag[1]=0;
    mx[0]=mx[1]=0;
    for(int i=1;i<=n;i++)
    {
        int lst[2],lmx[2];
        lst[0]=dp[a[i]][0]+tag[0],lst[1]=dp[a[i]][1]+tag[1];
        lmx[0]=mx[0],lmx[1]=mx[1];
        for(int c=0;c<2;c++)
        {
            tag[c]+=(a[i-1]==a[i])*b[a[i]];
            mx[c]+=(a[i-1]==a[i])*b[a[i]];
            int ndp=mx[c^1];
            chmax(ndp,lst[c^1]+b[a[i]]);
            chmax(dp[a[i-1]][c],ndp-tag[c]);
            chmax(mx[c],dp[a[i-1]][c]+tag[c]);
        }
    }
    int ans=max(mx[0],mx[1]);
    printf("%lld\n",ans);
}
signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%lld",&t);
    while(t--)solve();
    return 0;
}
//14:30.
